package com.sc.test.camera;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.SurfaceView;

import com.sc.test.R;
import com.sc.test.utils.Util;

/**
 * Created by Shen Chao
 * Created by 2019-03-06
 */
public class CustomSurfaceView extends SurfaceView {
//    private Paint paint1;
//    private Xfermode mXfermode;
//    private PorterDuff.Mode mPorterDuffMode = PorterDuff.Mode.SRC_OUT;
    public CustomSurfaceView(Context context) {
        this(context, null);
    }

    public CustomSurfaceView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        init();
    }

    private void init() {
//        paint1 = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
//        mXfermode = new PorterDuffXfermode(mPorterDuffMode);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        int height = getHeight();
        int width = getWidth();
        int ocrHeight = Util.dpToPx(getResources().getDimensionPixelOffset(R.dimen.ocr_height));
        int ocrWidth = Util.dpToPx(getResources().getDimensionPixelOffset(R.dimen.ocr_width));

        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        //新建矩形r1
        RectF r1 = new RectF();
        r1.left = width / 2 - ocrWidth / 2;
        r1.right = width / 2 + ocrWidth / 2;
        r1.top = height / 2 - ocrHeight / 2;
        r1.bottom = height / 2 + ocrHeight / 2;

//        RectF r2 = new RectF();
//        r2.left = 0;
//        r2.right = width;
//        r2.top = 0;
//        r2.bottom = height;

        int radus = Util.dpToPx(20);

        //背景色设为白色，方便比较效果
//        canvas.drawColor(Color.WHITE);
        //将绘制操作保存到新的图层，因为图像合成是很昂贵的操作，将用到硬件加速，这里将图像合成的处理放到离屏缓存中进行
//        int saveCount = canvas.saveLayer(r2, paint);
        //绘制目标图
        canvas.drawRoundRect(r1, radus, radus, paint);

        //设置混合模式
//        paint.setXfermode(mXfermode);
        //绘制源图
        //画出矩形r1
//        canvas.drawRoundRect(r2, 0, 0, paint);

        //清除混合模式
//        paint.setXfermode(null);
        //还原画布
//        canvas.restoreToCount(saveCount);

    }
}
